 package com.magist.core.util;

 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;

 public abstract class RawUtil
 {
   public RawUtil() {}
   
   public static abstract class CRCUtil
   {
     public CRCUtil() {}
     
     public static int getCRC16(byte[] data)
     {
/*  17 */       int[] CRCTABLE = { 61560, 57841, 54122, 49891, 46684, 42965, 38222, 33991, 31792, 28089, 24354, 20139, 14868, 
/*  18 */         11165, 6406, 2191, 57593, 61808, 50155, 53858, 42717, 46932, 34255, 37958, 27825, 32056, 20387, 24106, 
/*  19 */         10901, 15132, 2439, 6158, 53626, 49395, 62056, 58337, 38750, 34519, 46156, 42437, 23858, 19643, 32288, 
/*  20 */         28585, 6934, 2719, 14340, 10637, 49659, 53362, 58089, 62304, 34783, 38486, 42189, 46404, 19891, 23610, 
/*  21 */         28321, 32552, 2967, 6686, 10373, 14604, 45692, 41973, 37230, 32999, 62552, 58833, 55114, 50883, 15924, 
/*  22 */         12221, 7462, 3247, 30736, 27033, 23298, 19083, 41725, 45940, 33263, 36966, 58585, 62800, 51147, 54850, 
/*  23 */         11957, 16188, 3495, 7214, 26769, 31000, 19331, 23050, 37758, 33527, 45164, 41445, 54618, 50387, 63048, 
/*  24 */         59329, 7990, 3775, 15396, 11693, 22802, 18587, 31232, 27529, 33791, 37494, 41197, 45412, 50651, 54354, 
/*  25 */         59081, 63296, 4023, 7742, 11429, 15660, 18835, 22554, 27265, 31496, 29808, 26105, 22370, 18155, 12884, 
/*  26 */         9181, 4422, 207, 63544, 59825, 56106, 51875, 48668, 44949, 40206, 35975, 25841, 30072, 18403, 22122, 
/*  27 */         8917, 13148, 455, 4174, 59577, 63792, 52139, 55842, 44701, 48916, 36239, 39942, 21874, 17659, 30304, 
/*  28 */         26601, 4950, 735, 12356, 8653, 55610, 51379, 64040, 60321, 40734, 36503, 48140, 44421, 17907, 21626, 
/*  29 */         26337, 30568, 983, 4702, 8389, 12620, 51643, 55346, 60073, 64288, 36767, 40470, 44173, 48388, 13940, 
/*  30 */         10237, 5478, 1263, 28752, 25049, 21314, 17099, 47676, 43957, 39214, 34983, 64536, 60817, 57098, 52867, 
/*  31 */         9973, 14204, 1511, 5230, 24785, 29016, 17347, 21066, 43709, 47924, 35247, 38950, 60569, 64784, 53131, 
/*  32 */         56834, 6006, 1791, 13412, 9709, 20818, 16603, 29248, 25545, 39742, 35511, 47148, 43429, 56602, 52371, 
/*  33 */         65032, 61313, 2039, 5758, 9445, 13676, 16851, 20570, 25281, 29512, 35775, 39478, 43181, 47396, 52635, 
/*  34 */         56338, 61065, 65280 };
       
/*  36 */       int CRCVal = 0;
/*  37 */       int i = 0;
       
/*  39 */       for (i = 0; i < data.length; i++) {
/*  40 */         CRCVal = CRCTABLE[((CRCVal ^= data[i] & 0xFF) & 0xFF)] ^ CRCVal >> 8;
       }
       
/*  43 */       return CRCVal;
     }
     
     public static int crcTable(byte[] bytes) {
/*  47 */       int[] table = { 0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 
/*  48 */         50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 
/*  49 */         2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 
/*  50 */         56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 
/*  51 */         54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 
/*  52 */         63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 
/*  53 */         15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 
/*  54 */         60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 
/*  55 */         9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 
/*  56 */         41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 
/*  57 */         28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 
/*  58 */         47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 
/*  59 */         46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 
/*  60 */         37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 
/*  61 */         23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 
/*  62 */         34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 
/*  63 */         35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 
/*  64 */         32897, 16448 };
       
/*  66 */       int crc = 0;
       
/*  68 */       byte[] arrayOfByte = bytes;int j = bytes.length; for (int i = 0; i < j; i++) { byte b = arrayOfByte[i];
/*  69 */         crc = crc >>> 8 ^ table[((crc ^ b) & 0xFF)];
       }
       
/*  72 */       return crc;
     }
     
 
 
 
 
 
 
 
 
 
 
 
     public static final int evalCRC16(byte[] data)
     {
/*  88 */       int crc = 65535;
       
/*  90 */       for (int i = 0; i < data.length; i++) {
/*  91 */         crc = data[i] << 8 ^ crc;
         
/*  93 */         for (int j = 0; j < 8; j++) {
/*  94 */           if ((crc & 0x8000) != 0) crc = crc << 1 ^ 0x1021; else {
/*  95 */             crc <<= 1;
           }
         }
       }
/*  99 */       return (crc ^ 0xFFFF) & 0xFFFF;
     }
     
     public static int myCrc(byte[] data) {
/* 103 */       int result = 0;
/* 104 */       byte[] mydata = new byte[data.length + 2];
/* 105 */       result = mydata[0];
/* 106 */       int flagindex = 1;
/* 107 */       int index = 1;
/* 108 */       int temp = data[1];
       for (;;) {
/* 110 */         if ((result & 0x8000) == 0) {
/* 111 */           result ^= 0x8005;
         } else {
/* 113 */           if (flagindex == 5) {
/* 114 */             flagindex = 1;
/* 115 */             index++;
/* 116 */             if (index >= mydata.length) break;
/* 117 */             temp = mydata[index];
           }
/* 119 */           result <<= 1;
/* 120 */           temp <<= 1;
/* 121 */           result += temp / 256;
         }
       }
/* 124 */       return result;
     }
   }
   
 
 
 
 
 
 
   public static final Object deserialize(byte[] bytes)
     throws java.io.IOException, ClassNotFoundException
   {
/* 137 */     ObjectInputStream oi = new ObjectInputStream(new java.io.ByteArrayInputStream(bytes));
/* 138 */     Object o = oi.readObject();
/* 139 */     oi.close();
/* 140 */     return o;
   }
   
 
 
 
 
 
   public static final byte[] serialize(java.io.Serializable s)
     throws java.io.IOException
   {
/* 151 */     ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 152 */     ObjectOutputStream ot = new ObjectOutputStream(out);
/* 153 */     ot.writeObject(s);
/* 154 */     ot.flush();
/* 155 */     ot.close();
/* 156 */     return out.toByteArray();
   }
 }
